<?php

namespace  Ekadmin\Controller;
use \Think\Controller;
use Think\Page;

class MofangController extends CommonController
{






        public  function index()
        {


            $where['uniacid'] = $this->acid;


            $obj = M('mofang');
            $count = $obj->where($where)->count();
            $page = new Page($count);
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->select();
            $this->assign('page',$page->show());
            $this->assign('count',$count);
            $this->assign('list',$list);
            $this->display();
        }


        public function  edit()
        {
            $data = M('mofang')->find(I('post.id'));
            $arr = json_decode($data['url'],true);
            $data['url'] = $arr;
            //dump($data);die;
            $this->assign('data',$data);
            $this->display('form');
        }


        public function add()
        {
            $this->display('form');

        }


    public function del()
    {
        if(IS_POST){
            $param = I('id');

            if(is_array($param)){
                $map['id'] = array('in',$param);
            }else{
                $map['id'] = $param;
            }

            M('mofang')->where($map)->delete();

            ajax_return(0,'删除成功');
        }
    }


    public function update()
    {
        if(IS_POST){
            $param = I('post.');


            if(!$param['icon']){
                ajax_return(1,'请上传图片');
            }
            $json=[];
            if($param['type'] ==2){
                $json['url'] = I('post.url');
            }else if ($param['type'] == 3){
                $json['appid'] = I('post.appid');
                $json['path'] = I('post.path');
                $json['param'] = I('post.param');
            }

            $param['url'] = json_encode($json);
            $param['status'] = $param['status'] == 'on'?1:2;
            $obj = M('mofang');
            if($param['id']){
                $obj->save($param);
            }else{
                $param['addtime'] = time();
                $param['uniacid'] = $this->acid;
                $obj->add($param);
            }

            ajax_return(0,'保存成功',U('index'));
        }
    }





}